'use strict';

import format from 'date-fns/format'

/**
 * @class AppUtils 通用工具
 */
class AppUtils {
  /**
   * @method constructor
   */
  constructor() {
    Array.prototype.contains = function (obj) {
      var i = this.length;
      while (i--) {
        if (this[i] === obj) {
          return true;
        }
      }
      return false;
    }
    //字符串格式化
    String.prototype.format = function (args) {
      let result = this;
      if (arguments.length < 1) {
        return result.toString();
      }

      let data = arguments;
      if (arguments.length == 1 && typeof (args) == 'object') {
        data = args;
      }
      for (let key in data) {
        let value = data[key];
        if (undefined != value) {
          result = result.replace("{" + key + "}", value);
        }
      }
      return result.toString();
    }
    // 判断是否在给定时间范围内
    Date.prototype.inRange = function (tr) {
      if (tr[0].getTime() < this.getTime() && tr[1].getTime() > this.getTime()) {
        return true;
      }
      return false;
    };
    // 一天的毫秒数
    Date.prototype.oneDay = 1000 * 60 * 60 * 24;
    // 日期格式化
    Date.prototype.format = function (fmt) {
      return format(this, fmt);
      // let o = {
      //   "M+": this.getMonth() + 1, //月份
      //   "d+": this.getDate(), //日
      //   "h+": this.getHours(), //小时
      //   "m+": this.getMinutes(), //分
      //   "s+": this.getSeconds(), //秒
      //   "q+": Math.floor((this.getMonth() + 3) / 3), //季度
      //   "S": this.getMilliseconds() //毫秒
      // };
      // if (/(y+)/.test(fmt)) {
      //   fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      // }
      // for (let k in o) {
      //   if (new RegExp("(" + k + ")").test(fmt)) {
      //     fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      //   }
      // }
      // return fmt;
    }
    // 增加天
    Date.prototype.addDays = function (value) {
      if (value === undefined) value = 0;
      this.setDate(this.getDate() + value);
    }
    // 增加月
    Date.prototype.addMonths = function (value) {
      if (value === undefined) value = 0;
      this.setMonth(this.getMonth() + value);
    }
    // 增加年
    Date.prototype.addYears = function (value) {
      if (value === undefined) value = 0;
      this.setFullYear(this.getFullYear() + value);
    }
    // 设置距离当天天数的开始
    Date.prototype.setDayFirst = function (value) {
      if (value === undefined) value = 0;
      this.setTime((new Date(this.format('MM DD,YYYY') + ' 00:00:00.000')).getTime());
      this.setTime(this.getTime() + this.oneDay * value);
    }
    // 设置当天天数的结束
    Date.prototype.setDayLast = function (value) {
      if (value === undefined) value = 0;
      this.setTime((new Date(this.format('MM DD,YYYY') + ' 23:59:59.999')).getTime());
      this.setTime(this.getTime() + this.oneDay * value);
    }
    // 设置日期到本周第n天
    Date.prototype.setWeek = function (value) {
      if (value === undefined) value = 0;
      let year = this.getFullYear();
      //因为月份是从0开始的,所以获取这个月的月份数要加1才行
      let month = this.getMonth() + 1;
      let date = this.getDate();
      let day = this.getDay();
      console.log(date);
      //判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
      if (day !== 0) {
        value = value + (day - 1);
      } else {
        value = value + day;
      }
      if (day) {
        //这个判断是为了解决跨年的问题
        if (month > 1) {
          month = month;
        }
        //这个判断是为了解决跨年的问题,月份是从0开始的
        else {
          year = year - 1;
          month = 12;
        }
      }
      this.setDate(this.getDate() - value);
      year = this.getFullYear();
      month = this.getMonth() + 1;
      date = this.getDate();
      let newData = new Date(year, month - 1, date, this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
      this.setTime(newData.getTime());
    }
    // 设置距离本周周数的开始
    Date.prototype.setWeekFirst = function (value) {
      if (value === undefined) value = 0;
      this.setWeek(1);
      this.setDayFirst(-7 * value);
    }
    // 设置本周周数的结束
    Date.prototype.setWeekLast = function (value) {
      if (value === undefined) value = 0;
      this.setWeek(7);
      this.setDayLast(-7 * value);
    }
    // 设置距离当月的第一天
    Date.prototype.setMonthFirst = function (value) {
      if (value === undefined) value = 0;
      this.setDate(1);
      this.setMonth(this.getMonth() + value);
      this.setDayFirst();
    }
    // 设置距离当月的最后一天
    Date.prototype.setMonthLast = function (value) {
      if (value === undefined) value = 0;
      let currentMonth = this.getMonth();
      let nextMonth = ++currentMonth;
      let nextMonthFirstDay = new Date(this.getFullYear(), nextMonth, 1);
      this.setTime((new Date(nextMonthFirstDay - this.oneDay)).getTime());
      this.setMonth(this.getMonth() + value);
      this.setDayLast();
    }
    // 设置距离今年年数的开始
    Date.prototype.setYearFirst = function (value) {
      if (value === undefined) value = 0;
      this.setTime((new Date(this.format('01 01,YYYY') + ' 00:00:00.000')).getTime());
      this.setFullYear(this.getFullYear() + value);
    }
    // 设置距离今年年数的结束
    Date.prototype.setYearLast = function (value) {
      if (value === undefined) value = 0;
      this.setTime((new Date(this.format('12 31,YYYY') + ' 23:59:59.999')).getTime());
      this.setFullYear(this.getFullYear() + value);
    }
    Date.prototype.parseRange = function (day) {
      let ts = new Date(this.getTime());
      let te = new Date(this.getTime());
      switch (day) {
        case '今天':
          ts.setDayFirst();
          te.setDayLast();
          break;
        case '昨天':
          ts.setDayFirst(-1);
          te.setDayLast(-1);
          break;
        case '前天':
          ts.setDayFirst(-2);
          te.setDayLast(-2);
          break;
        case '这周':
        case '这个星期':
          ts.setWeekFirst();
          ts.setWeekLast();
          break;
        case '上个星期':
          ts.setWeekFirst(-1);
          ts.setWeekLast(-1);
          break;
        case '上上个星期':
          ts.setWeekFirst(-2);
          ts.setWeekLast(-2);
          break;
        case '这个月':
          ts.setMonthFirst();
          te.setMonthLast();
          break;
        case '上个月':
          ts.setMonthFirst(-1);
          te.setMonthLast(-1);
          break;
        case '上上月':
        case '上上个月':
          ts.setMonthFirst(-2);
          te.setMonthLast(-2);
          break;
        case '今年':
          ts.setYearFirst();
          te.setYearLast();
          break;
        case '去年':
        case '昨年':
          ts.setYearFirst(-1);
          te.setYearLast(-1);
          break;
        case '前年':
          ts.setYearFirst(-2);
          te.setYearLast(-2);
          break;
        default:
          // 默认今天
          day = '今天';
          ts.setDayFirst();
          te.setDayLast();
          break;
      }
      return [ts, te];
    }
  }

  // 生成 uuid
  genUuid() {
    let s = [];
    let hexDigits = "0123456789abcdef";
    for (let i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23] = "-";

    let uuid = s.join("");
    return uuid;
  }
}

export default AppUtils;
